package com.wangyadong.hobby.schedule.jobtask.javasync;

import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.ConcurrentLinkedDeque;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.IntStream;

/**
 * <p>
 * 为什么需要sync
 *
 * @author <714037465@qq.com>
 * @since 2018/11/1317:08
 **/
public class ThreadExample extends Thread {
    static final ConcurrentLinkedDeque<Integer> queue = new ConcurrentLinkedDeque<>();
    private static final int MAX = 500;
    private static int init = 0;


    /**
     * 模拟售票大厅
     */
    @Override
    public void run() {
        synchronized (ThreadExample.class) {
            while (init < MAX) {
                int i = init++;
                System.out.println(Thread.currentThread().getName() + " add--> " + i);
                queue.add(i);
            }
        }
    }

    public static void main(String[] args) {
        IntStream.range(0, 100).forEach(i -> {
            ThreadExample threadExample = new ThreadExample();
            threadExample.start();
        });
        try {
            TimeUnit.SECONDS.sleep(2);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        //我们期望他的大小为500
        System.out.println(queue.size());

    }
}
